/* 
 * The MIT License
 *
 * Copyright 2014 Venkat Ram Akkineni.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.netbeans.modules.tfsnb.core;

import com.microsoft.tfs.core.TFSTeamProjectCollection;
import static org.netbeans.modules.tfsnb.core.TFSModuleConfig.DELIMITER;
import static org.netbeans.modules.tfsnb.core.Util.isNotEmpty;

/**
 *
 * @author Venkat Ram Akkineni
 */
public class ConnectionSettings {

    
    public static enum PROTOCOL { 
    
        HTTP, HTTPS;
        
        public static PROTOCOL getProtocol(String protocolString) {
            switch (protocolString.toLowerCase()) {
                case "http":
                    return HTTP;
                case "https":
                    return HTTPS;
                default:
                    throw new IllegalArgumentException("Bad string representation of a protocol provided.");
            }
        }
    
    }
    
    private String id;
    private String name;
    private String serverUrl;
    private String path;
    private String portNumber;
    private PROTOCOL protocol;
    private String username;
    private char[] password;
    private boolean saveCredentials  = false;
    
    private transient TFSTeamProjectCollection teamProjectCollection = null;

    public ConnectionSettings() {
    }
    
    public ConnectionSettings(final String persistedConnection, final String id) {
        this.id = id;
        String[] connectionComponents = persistedConnection.split(TFSModuleConfig.DELIMITER);
        if(connectionComponents.length == 5) {
            saveCredentials = false;            
        } else if(connectionComponents.length == 7) {
            saveCredentials = true;
            username = connectionComponents[5];
            password = connectionComponents[6].toCharArray();
        } else {
            throw new IllegalArgumentException("Connection was badly persisted.");
        }
        
        if(connectionComponents.length == 5 || connectionComponents.length == 7) {
            name = connectionComponents[0];
            serverUrl = connectionComponents[1];
            path = connectionComponents[2];
            portNumber = connectionComponents[3];            
            protocol = PROTOCOL.getProtocol(connectionComponents[4]);
        }
    }
    
    public ConnectionSettings(final String name, final String serverUrl, final String path, 
            final String portNumber, final PROTOCOL protocol, final boolean saveCredentials) {
        this.name = name;
        this.serverUrl = serverUrl;
        this.path = path;
        this.portNumber = portNumber;
        this.protocol = protocol;
        this.id = name;
        this.saveCredentials = saveCredentials;
    }
    
    public ConnectionSettings(final String name, final String serverUrl, final String path, final String portNumber, 
            final PROTOCOL protocol, final String username, final String password, final boolean saveCredentials) {
        this(name, serverUrl, path, portNumber, protocol, saveCredentials);
        setUsername(username);
        setPassword(password.toCharArray());
    }
    
    public boolean validate() {        
        if(saveCredentials && isNotEmpty(new String[]{ name, serverUrl, path, portNumber, id, username, new String(password) })) {
            return true;
        } else if(!saveCredentials && isNotEmpty(new String[]{ name, serverUrl, path, portNumber, id })) {
            return true;
        }        
        return false;
    }
    
    public String buildFullUrl() {
        String urlPreview = "";
        if(validate()) {
            if(protocol != null) {
                urlPreview += protocol.toString().toLowerCase();
            }        
            if(isNotEmpty(serverUrl)) {
                urlPreview += "://" + serverUrl;
            }        
            if(isNotEmpty(portNumber)) {
                urlPreview += ":" + portNumber;
            }
            if(isNotEmpty(path)) {
                urlPreview += "/" + path;
            }
        } 
                
        return urlPreview;
    }

    @Override
    public String toString() {
        StringBuilder builder = new StringBuilder();
        builder.append(name);
        builder.append(DELIMITER);
        builder.append(serverUrl);
        builder.append(DELIMITER);
        builder.append(path);
        builder.append(DELIMITER);
        builder.append(portNumber);
        builder.append(DELIMITER);
        builder.append(protocol);
        if(saveCredentials) {
            builder.append(DELIMITER);
            builder.append(username);
            builder.append(DELIMITER);
            builder.append(password);        
        }
        
        return builder.toString();
    }
    
    public static ConnectionSettings parse (final String[] s, final String id) {
        return new ConnectionSettings(s[0].trim(), id);
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getServerUrl() {
        return serverUrl;
    }

    public void setServerUrl(String serverUrl) {
        this.serverUrl = serverUrl;
    }

    public String getPath() {
        return path;
    }

    public void setPath(String path) {
        this.path = path;
    }

    public String getPortNumber() {
        return portNumber;
    }

    public void setPortNumber(String portNumber) {
        this.portNumber = portNumber;
    }

    public PROTOCOL getProtocol() {
        return protocol;
    }

    public void setProtocol(PROTOCOL protocol) {
        this.protocol = protocol;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public char[] getPassword() {
        return password;
    }

    public void setPassword(char[] password) {
        this.password = password;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public boolean isSaveCredentials() {
        return saveCredentials;
    }

    public void setSaveCredentials(boolean saveCredentials) {
        this.saveCredentials = saveCredentials;
    }

    public TFSTeamProjectCollection getTeamProjectCollection() {
        return teamProjectCollection;
    }

    public void setTeamProjectCollection(TFSTeamProjectCollection teamProjectCollection) {
        this.teamProjectCollection = teamProjectCollection;
    }
    
}
